package com.atguigu.controller;


import ch.qos.logback.classic.spi.EventArgUtil;
import com.atguigu.domain.Product;
import com.atguigu.service.IProductService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

@RestController
@RequestMapping("/api/product")
public class ProductController {

    @Autowired
    private IProductService productService;



    /**
     * 根据ID获取单个产品
     * @param id 产品ID
     * @return 产品信息或404
     */
    @GetMapping("/{id}")
    public ResponseEntity<Product> getProductById(@PathVariable String id, HttpServletRequest request ) {

        String token = request.getHeader("token");

        System.out.println("发起请求，请求的token是"+token);
        Optional<Product> product = productService.getProductById(id);
        return product.map(ResponseEntity::ok)
                .orElseGet(() -> ResponseEntity.notFound().build());
    }


}
